package lambdasinaction.chap11.v1;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class ShopMain {

  public static void main(String[] args) {
    Shop shop = new Shop("BestShop");
    long start = System.nanoTime();
    Future<Double> futurePrice = shop.getPriceAsync("我最喜欢的产品");
    long invocationTime = ((System.nanoTime() - start) / 1_000_000);
    System.out.println("调用返回后" + invocationTime  + " msecs");

    // 做一些更多的任务，如查询其他商店
    doSomethingElse();
    // 虽然产品的价格正在计算
    try {
        double price = futurePrice.get();
        System.out.printf("价格是 %.2f%n", price);
    } catch (ExecutionException | InterruptedException e) {
        throw new RuntimeException(e);
    }
    long retrievalTime = ((System.nanoTime() - start) / 1_000_000);
    System.out.println("返回后的价格 " + retrievalTime + " msecs");
  }

  private static void doSomethingElse() {
      System.out.println("做别的事情…");
  }

}
